package top.maof.book.service.myImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.maof.book.dao.HistoryMapper;
import top.maof.book.model.History;
import top.maof.book.result.PageData;
import top.maof.book.result.PageWrap;
import top.maof.book.service.HistoryService;
import top.maof.book.utils.Utils;

import java.util.List;

@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {
    @Autowired
    HistoryMapper historyMapper;

    @Override
    public PageData<History> findPage(PageWrap<History> pageWrap) {
        IPage<History> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<History> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getId() != null) {
            queryWrapper.lambda().eq(History::getId, pageWrap.getModel().getId());
        }
        if (pageWrap.getModel().getUserId() != null) {
            queryWrapper.lambda().eq(History::getUserId, pageWrap.getModel().getUserId());
        }
        if (pageWrap.getModel().getBookId() != null) {
            queryWrapper.lambda().eq(History::getBookId, pageWrap.getModel().getBookId());
        }
        if (pageWrap.getModel().getContentsId() != null) {
            queryWrapper.lambda().eq(History::getContentsId, pageWrap.getModel().getContentsId());
        }
        if (pageWrap.getModel().getChapterId() != null) {
            queryWrapper.lambda().eq(History::getChapterId, pageWrap.getModel().getChapterId());
        }
        if (pageWrap.getModel().getIp() != null) {
            queryWrapper.lambda().eq(History::getIp, pageWrap.getModel().getIp());
        }
        if (pageWrap.getModel().getInsertDate() != null) {
            queryWrapper.lambda().ge(History::getInsertDate, Utils.Date.getStart(pageWrap.getModel().getInsertDate()));
            queryWrapper.lambda().le(History::getInsertDate, Utils.Date.getEnd(pageWrap.getModel().getInsertDate()));
        }
        for (PageWrap.SortData sortData : pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(historyMapper.selectPage(page, queryWrapper));
    }

    @Deprecated
    @Override
    public List<History> findHistoriesByUser(Integer userId) {
        return historyMapper.findHistoriesByUser(userId);
    }

    @Deprecated
    @Override
    public int clickCount(int bookId) {
        return historyMapper.clickCount(bookId);
    }

    @Deprecated
    @Override
    public int add(History history) {
        return historyMapper.add(history);
    }
}
